package net.sxnic.item.action;

import java.util.HashSet;
import java.util.Set;

import net.sxinfo.core.dao.Page;
import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.dao.hibernate3.HibernateDaoUtils;
import net.sxinfo.core.dao.hibernate3.HibernateOrder;
import net.sxinfo.core.util.WebUtils;
import net.sxnic.item.mdata.action.MdataAction;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.security.AuthorizationManager;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.user.UserManager;
import net.sxnic.ugr.utils.UcenterUtils;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("serial")
public class ApproverCreate extends MdataAction {
	
	private static String SYMBOL = "-";
	User user;
	@Autowired
	UserManager userManager;
	@Autowired
	AuthorizationManager authorizationManager;
	
	public String execute() throws Exception {
		
		User currentUser = ucenterService.findUserByUserName(AppContext
				.getUserName(request));
		String userPrefix = currentUser.getUserOrder()+SYMBOL;
		String tempPass = "";
		Role role = authorizationManager.getRole("approver");
		user = new User();
		user.setUsername(userManager.findMaxByUserPrefix(userPrefix));
		tempPass = UcenterUtils.getRadomNum(6);
		user.setPassword(DigestUtils.md5Hex(tempPass));
		user.setPlainPassword(tempPass);
		user.addRole(role);
		userManager.save(user);
		msg = "createSuccess";
		return SUCCESS;
	}
	public Page getDatas() {
		return datas;
	}
	public void setDatas(Page datas) {
		this.datas = datas;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
}
